package com.esincrunch.tool;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;

public  class Preferences extends PreferenceActivity implements 
OnSharedPreferenceChangeListener {
	PreferenceCategory pc;
	ListPreference lp = null;
	EditTextPreference etp1;
	EditTextPreference etp2;
	EditTextPreference etp3;
    String TAG="Preferences";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		prefs.registerOnSharedPreferenceChangeListener(this);
		
		lp = (ListPreference) findPreference("carrier_choice");
		pc = (PreferenceCategory) findPreference("pc_carrier_choice");
		etp1=(EditTextPreference)findPreference("cmno");
		etp2=(EditTextPreference)findPreference("cuno");
		etp3=(EditTextPreference)findPreference("ctno");
	}
	@Override
	protected void onResume(){
		super.onResume();
		lp.setValueIndex(0);
		setListPreSumm(lp,-1);
	}
	@Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
        // TODO Auto-generated method stub
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }
    
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
    	
    	int idx=lp.findIndexOfValue(lp.getValue());
        if (key.equals("carrier_choice")){
        	setListPreSumm(lp,-1);
        }
        else if(key.equals("cmno")){
        	setListPreEntryValue(0,idx,etp1.getText());
        }
        else if (key.equals("cuno")){
        	setListPreEntryValue(1,idx,etp2.getText());
        }
        else if (key.equals("ctno")){
        	setListPreEntryValue(2,idx,etp3.getText());
        }
    }
    
    private void setListPreSumm(ListPreference lp, int idx){
    	if(idx==-1){
    		idx = lp.findIndexOfValue(lp.getValue());
    	}
    	String lpText=null;
    	lpText=lp.getEntries()[idx].toString();
    	lp.setSummary(lpText+"("+lp.getValue().toString()+")");	
    }
    
    private void setListPreEntryValue(int postion,int select,String etpValue){
    	Log.e(TAG,etpValue);
    	Log.e(TAG,lp.getEntryValues()[postion].toString());
    	CharSequence[] entryValues=lp.getEntryValues();
    	entryValues[postion]=etpValue;
    	lp.setEntryValues(entryValues);
    	lp.setValueIndex(select);
    	setListPreSumm(lp,select);
    }

}
